﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AllIsWell.Persistance;
using AllIsWell.Persistance.Ef;
using AllIsWell.Services;
using AllIsWell.Services.Infrastructure;
using Ninject;

namespace AllIsWell.Web
{
    public class NinjectDependencyResolver : IDependencyResolver
    {
        private readonly IKernel _ninjectKernel;
        private readonly IUnitOfWork _unitOfWork;

        public NinjectDependencyResolver()
        {
            _ninjectKernel = new StandardKernel();
            _unitOfWork = new UnitOfWork();
            AddBindings();
        }

        private void AddBindings()
        {
            const string serviceConstructorArgumentName = "unitOfWork";

            _ninjectKernel.Bind<IDestinyService>().To<DestinyService>()
                                                  .WithConstructorArgument(serviceConstructorArgumentName, _unitOfWork);
            _ninjectKernel.Bind<IPageService>().To<PageService>()
                                                  .WithConstructorArgument(serviceConstructorArgumentName, _unitOfWork);
            _ninjectKernel.Bind<IWhatService>().To<WhatService>()
                                                  .WithConstructorArgument(serviceConstructorArgumentName, _unitOfWork);
        }

        public object GetService(Type serviceType)
        {
            return _ninjectKernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _ninjectKernel.GetAll(serviceType);
        }
    }

}